/*
 * @Author: Otway
 * @Date: 2019-01-09 11:16:08
 * @LastEditors: Otway
 * @LastEditTime: 2019-08-30 18:50:09
 * @copyright: h-visions
 */
import React from 'react';
import { openModalMessage } from '../../../components/TableCustomize'
import * as BillManageService from '../../../axios/BillManage'
import {
    Form,
    Select,
    Input,
    DatePicker
} from 'antd';
import moment from 'moment';
const FormItem = Form.Item;
const { Option } = Select
const { TextArea } = Input

const AddFrom = ({ form: { getFieldDecorator, getFieldValue, setFieldsValue }, ReportList, ErrorList, ResultList, departmentList, batchNoList, uniqueCode }) => {

    let batchInfo = {}
    const onHandleChange = (value) => {
        // let a = batchNoList.find(e => e.batchNo == value)
        // if (a) {
        //     setFieldsValue({ model: a.model })
        // }

        BillManageService.findAbnormalBatchNo(`${value}`).then(res => {
            if (res.data.code !== 200) {
                openModalMessage('warning', res.data.msg)
                return
            }
            batchInfo = res.data?.data
            // console.log(batchInfo, 'batchInfo');
            setFieldsValue({
                model: batchInfo?.model,
                assembleStartTime: batchInfo?.assembleStartTime ? moment(batchInfo?.assembleStartTime) : undefined,
                department: batchInfo?.department,
                firstDeliveryTime: batchInfo?.firstDeliveryTime ? moment(batchInfo?.firstDeliveryTime) : undefined,
                firstOutputTime: batchInfo?.firstOutputTime ? moment(batchInfo?.firstOutputTime) : undefined
            })
        })
    }
    const formItemLayout = {
        // 抽屉里面Form。二列的布局比例
        labelCol: {
            span: 8
        },
        wrapperCol: {
            span: 12
        }
    };
    return (
        <Form>
            <FormItem {...formItemLayout} label={'单据号'}>
                {getFieldDecorator('uniqueCode', {
                    initialValue: uniqueCode,
                    rules: [
                        {
                            required: true,
                            message: '请输入单据号'
                        }
                    ],
                    getValueFromEvent: (event) => {
                        return event.target.value.replace(/\s+/g, "")
                    },
                })(<Input placeholder='请输入单据号' disabled allowClear />)}
            </FormItem>
            <FormItem {...formItemLayout} label={'报告类别'}>
                {getFieldDecorator('reportType', {
                    // initialValue: formData.abnormalMsg || undefined,
                    rules: [
                        {
                            required: true,
                            message: '请选择报告类别'
                        }
                    ]
                })(<Select
                    placeholder='请选择报告类别'
                    allowClear
                    showSearch
                    filterOption={(input, option) =>
                        option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    }
                >
                    {ReportList && ReportList.map(item => <Option key={item.value}>{item.value}</Option>)}
                </Select>)}
            </FormItem>
            <FormItem {...formItemLayout} label={'批号'}>
                {getFieldDecorator('batchNo', {
                    rules: [
                        {
                            required: true,
                            message: '请选择批号'
                        }
                    ]
                })(
                    <Select
                        placeholder='请选择批号'
                        allowClear
                        showSearch
                        filterOption={(input, option) =>
                            option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                        }
                        onChange={onHandleChange}
                    >
                        {batchNoList && batchNoList.map(item => <Option key={item.batchNo}>{item.batchNo}</Option>)}
                    </Select>
                )}
            </FormItem>
            <FormItem {...formItemLayout} label={'机型'}>
                {getFieldDecorator('model', {
                    initialValue: batchInfo?.model,
                    getValueFromEvent: (event) => {
                        return event.target.value.replace(/\s+/g, "")
                    },
                })(<Input disabled allowClear />)}
            </FormItem>
            <FormItem {...formItemLayout} label={'总装车间'}>
                {getFieldDecorator('department', {
                    initialValue: batchInfo?.department,
                    rules: [
                        {
                            required: true,
                            message: '请输入总装车间'
                        }
                    ],
                    getValueFromEvent: (event) => {
                        return event.target.value.replace(/\s+/g, "")
                    },
                })(
                    <Input placeholder='请输入总装车间' allowClear disabled />
                    // <Select
                    //     placeholder='请选择总装车间'
                    //     allowClear
                    //     showSearch
                    //     filterOption={(input, option) =>
                    //         option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    //     }
                    // >
                    //     {departmentList && departmentList.map(item => <Option key={item.workShop}>{item.workShop}</Option>)}
                    // </Select>
                )}
            </FormItem>
            {/* <FormItem {...formItemLayout} label={'装配机号'}>
                {getFieldDecorator('fromCode', {
                    rules: [
                        {
                            required: true,
                            message: '请输入装配机号'
                        }
                    ]
                })(<Input placeholder='请输入装配机号' allowClear />)}
            </FormItem> */}
            <FormItem {...formItemLayout} label={'装配开始时间'}>
                {getFieldDecorator('assembleStartTime', {
                    initialValue: batchInfo?.assembleStartTime || undefined,
                })(
                    <DatePicker allowClear disabled />
                )}
            </FormItem>
            <FormItem {...formItemLayout} label={'第一交机时间'}>
                {getFieldDecorator('firstDeliveryTime', {
                    initialValue: batchInfo?.firstDeliveryTime || undefined,
                })(
                    <DatePicker allowClear disabled />
                )}
            </FormItem>
            <FormItem {...formItemLayout} label={'第一出机时间'}>
                {getFieldDecorator('firstOutputTime', {
                    initialValue: batchInfo?.firstOutputTime || undefined,
                })(
                    <DatePicker allowClear disabled />
                )}
            </FormItem>
            {/* {
                getFieldValue('reportType') == '交机' ? '交机'
                    :
                    getFieldValue('reportType') == '出机' ? '出机'
                        : null
            } */}
            <FormItem {...formItemLayout} label={'异常原因'}>
                {getFieldDecorator('abnormalMsg', {
                    rules: [
                        {
                            required: true,
                            message: '请选择异常原因'
                        }
                    ]
                })(<Select
                    placeholder='请选择异常原因'
                    allowClear
                    showSearch
                    filterOption={(input, option) =>
                        option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    }
                >
                    {ErrorList && ErrorList.map(item => <Option key={item.value}>{item.value}</Option>)}
                </Select>)}
            </FormItem>
            {/* 如果异常原因选择其它原因，具体原因必填； */}
            <FormItem {...formItemLayout} label={'具体原因'}>
                {getFieldDecorator('fromCode', {
                    rules: [
                        {
                            required: getFieldValue('abnormalMsg') == '其他' ? true : false,
                            message: '请输入具体原因'
                        }
                    ]
                })(<Input placeholder='请输入具体原因' allowClear />)}
            </FormItem>
            <FormItem {...formItemLayout} label={'异常内容'}>
                {getFieldDecorator('contentMsg', {
                })(<TextArea placeholder='请输入异常内容' allowClear autoSize={{ minRows: 3, maxRows: 10 }} />)}
            </FormItem>
        </Form>
    );

}

export default Form.create()(AddFrom);
